package com.example.emojibase.view;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.emojibase.EmojiBaseDB;
import com.example.emojibase.R;
import com.example.emojibase.adapter.EmojiAdapter;
import com.example.emojibase.pojo.Emoji;

public class EmojiDetailOnline extends AppCompatActivity {
    private Emoji emoji;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emoji_detail_online);
        int position = getIntent().getIntExtra("ITEM_POSITION", -1);
        ImageView img=findViewById(R.id.imageView);
        EmojiAdapter emojiAdapter = EmojiAdapter.getInstance(this);
        emoji= emojiAdapter.getEmojiByPosition(position);
        Bitmap imagebitmap = BitmapFactory.decodeByteArray(emoji.getAvatar(), 0, emoji.getAvatar().length);
        img.setImageBitmap(imagebitmap);
        Button saveButton = findViewById(R.id.saveButton);

        saveButton.setOnClickListener(view -> {
            SQLiteDatabase db=EmojiBaseDB.getInstance(this).getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put("name", emoji.getName());
            cv.put("avatar", emoji.getAvatar());//图片转为二进制
            db.insert("emoji", null, cv);
        });
    }

}
